//
//  WYWAlertView.m
//  15WClient
//
//  Created by JinYong on 16/5/15.
//  Copyright © 2016年 15w. All rights reserved.
//

#import "WYWAlertView.h"
static const NSTimeInterval kAlertDurationTimeInterval  = 0.6f;

@implementation WYWAlertView
+ (void)showAlertQuiet:(NSString *)title {
    [WYWAlertView showAlertQuiet:title totalDuration:kAlertDurationTimeInterval];
}

+ (void)showErrorAlertQuiet:(NSError *)error {
    if (error.code == kTokenErrorCode || error.code == kTokenExpireCode) {
        [WYWAlertView showAlertQuiet:kTokenExpireMsg];
//        [[NJUserInstance sharedInstance] logout];
//        [[NSNotificationCenter defaultCenter] postNotificationName:WYW_NOTI_BACKHOME object:nil];
    }
    else {
        NSString *strAlert = @"";
        strAlert = error.userInfo[kErrorMsgKey];
        if (strAlert.length == 0) {
            strAlert = [error localizedDescription];
        }
        [WYWAlertView showAlertQuiet:strAlert totalDuration:kAlertDurationTimeInterval];
    }
}

+ (void)showAlertQuiet:(NSString *)title totalDuration:(NSTimeInterval)duration {
    
    UILabel *tipsLbl = (UILabel *)[[[[UIApplication sharedApplication] delegate] window] viewWithTag:7451];
    if (tipsLbl) {
        [tipsLbl removeFromSuperview];
        tipsLbl = nil;
    }
    tipsLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 30)];
    tipsLbl.backgroundColor = RGBACOLOR(0, 0, 0, 0.8);
    tipsLbl.font = fontWithPingfang(14);
    tipsLbl.text = title;
    tipsLbl.numberOfLines = 0;
    [tipsLbl sizeToFit];
    CGRect frame = tipsLbl.frame;
    if (frame.size.width<SCREEN_W/4-20) {
        tipsLbl.frame = CGRectMake(0, 0,SCREEN_W/4, tipsLbl.frame.size.height + 20);
    }
    else{
        tipsLbl.frame = CGRectMake(0, 0, tipsLbl.frame.size.width + 20, tipsLbl.frame.size.height + 20);
        
    }
    tipsLbl.textColor = [UIColor whiteColor];
    tipsLbl.textAlignment = NSTextAlignmentCenter;
    tipsLbl.tag = 7451;
    tipsLbl.center = CGPointMake(SCREEN_W/2, SCREEN_H/2);
    tipsLbl.layer.cornerRadius = 5.0f;
    tipsLbl.layer.masksToBounds = YES;
    
    tipsLbl.alpha = 0.0f;
    [[[[UIApplication sharedApplication] delegate] window] addSubview:tipsLbl];
    [UIView animateWithDuration:duration/2 animations:^{
        tipsLbl.alpha = 1.0f;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:duration/2 delay:1 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            tipsLbl.alpha = 0.0f;
        } completion:^(BOOL finished) {
            [tipsLbl removeFromSuperview];
        }];
    }];
}

@end
